using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Windows.Forms;
using RESTAgent;
using Tavis;
using Tavis.Interfaces;
using RESTShell.Missions;


namespace RESTShell.Shell {
    public class ShellModel {
        private RestAgent _RestAgent;

        public event Action OnContentChanging = delegate { };
        public event Action OnContentChanged = delegate { };

        public CompositionContainer Container { get; set; }
		public IHypermediaContent CurrentContent { get; set; }

		public string CurrentMediaType { get; private set; }
		public ILink CurrentLocation { get; private set; }


        public RestAgent RestAgent {
            get { return _RestAgent; }
            set { _RestAgent = value;
                CurrentContent = _RestAgent.CurrentContent;
				CurrentMediaType = _RestAgent.CurrentContent.MediaType;
				CurrentLocation = _RestAgent.CurrentLocation;
            }
        }

		public IMission DefaultMission { get; set; }

		
        internal void InitClientState() {

			_RestAgent.RegisterDefaultResponseStartHandler(e => {
				lock (e.ResponseMessage.Content.Headers) {
					CurrentMediaType = e.ResponseMessage.Content.Headers.ContentType.MediaType;
				}
				CurrentLocation = e.SourceLink;

				OnContentChanging();
			});

			_RestAgent.RegisterDefaultResponseCompleteHandler(content => {
				CurrentContent = content;

				OnContentChanged();
			});



            //Assembly a = Assembly.GetExecutingAssembly();
            //_RestAgent.AddProductToUserAgent(new ProductHeaderValue("RESTShell", a.GetName().Version.ToString()));
			// _RestAgent.NavigateTo(new Link(RootUri));
        }
    }
}
